package ua.yandex.skipass;

import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author Кирилл
 */
public class SkiPassSeasonCard extends SkiPassCard {

    private static final int MONTH_IN_SEASON = 3;
    
    SkiPassSeasonCard(Date activationDate, Date expirationDate, int id) {
        super(activationDate, expirationDate, id, SkiPassType.SEASON);
        if (activationDate.getTime() > expirationDate.getTime()) {
            throw new IllegalSkiPassSystemException(
                      "This card will be activated after it has expired");
        }
        if (getTheEndOfCurrentSeason(activationDate).getTime() <
                expirationDate.getTime()) {
            throw new OutOfBoundException(
                      "Duration of required season ticket exceed max possible");
        }
    }
    
    SkiPassSeasonCard(Date activationDate, int id) {
        super(activationDate, getTheEndOfCurrentSeason(activationDate),
              id, SkiPassType.SEASON);
    }
    
    SkiPassSeasonCard(int id) {
        super(new Date(System.currentTimeMillis()), getTheEndOfCurrentSeason(), 
                id, SkiPassType.SEASON);
    }
    
    private static Date getTheEndOfCurrentSeason(Date activationDate) {
        Date currentDate = activationDate;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.MONTH, MONTH_IN_SEASON);
        Date expirationDate = calendar.getTime();
        return expirationDate;
    }
    private static Date getTheEndOfCurrentSeason() {
        return getTheEndOfCurrentSeason(new Date(System.currentTimeMillis()));
    }
    
    @Override
    public boolean passIsAllowed() {
        System.out.println(isBlocked());
        System.out.println(isExpired());
        return !(isBlocked() || isExpired());
    }

}
